package com.audaque.springboot.foshanupload.web.webdemo.main;

/**
 * @author zgb
 * @desc ...
 * @date 2023-08-27 13:47:33
 */
public class DaemonThread {
    public static void main(String[] args) {
        Thread daemonThread = new Thread(new DaemonTask());
        daemonThread.setDaemon(true); // 将线程设置为守护线程
        daemonThread.start();

        // 主线程执行一段时间
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Main thread finished.");
    }

    static class DaemonTask implements Runnable {
        @Override
        public void run() {
            while (true) {
                System.out.println("Daemon thread is running.");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
